import { API_BASE_URL } from '@/config/setting';
import axios from 'axios';
import request from '@/utils/request';
import { message } from 'ant-design-vue';
import { getToken, setToken } from './token-util';

export function daochuFn(data) {
  axios({
    method: 'post',
    url: API_BASE_URL + data.url,
    responseType: 'blob',
    headers: {
      Authorization: getToken()
    },
    timeout: 8000,
    data: data.params
  })
    .then((res) => {
      const link = document.createElement('a');
      const blob = new Blob([res.data], {
        type: 'application/vnd.ms-excel'
      });
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);

      let fileName = decodeURI(
        res.headers['content-disposition'].split(';')[1].split('=')[1]
      );
      link.download = fileName.replace(new RegExp('"', 'g'), ''); //解决文件名最前最后多出来的下划线问题 https://blog.csdn.net/qq_41732963/article/details/122211939
      console.log('文件名', link.download);
      //link.download = data.filename; //下载的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      message.success('导出成功！');
    })
    .catch((error) => {
      message.error('参数错误！');
    });
}
